# 生产岗位 技术相关的词云
import os
import django
from PIL import ImageFont
from tqdm import tqdm

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "JobViewer.settings")
django.setup()
from Apps.Data.models import JobModel
from wordcloud import WordCloud

joblist = JobModel.objects.all()
dist = {}
total = 0
for job in joblist:
    total += 1
    skills = job.skills.split(',')
    for skill in skills:
        if skill not in dist:
            dist[skill] = 1
        else:
            dist[skill] += 1
# print(dist)
from pprint import pprint

for (skill, count) in dist.items():
    dist[skill] = count / total
pprint(dist)

# 设置相关字体 --- 防止中文乱码
import os , sys
current_path = os.path.dirname(os.path.abspath(sys.argv[0]))
print(current_path)
from pathlib import Path
parent_path = Path(current_path).parent
target_path = str(parent_path) + '/Utils/font/SmileySans-Oblique.ttf'
# /Users/hnitro/Desktop/job_viewer_backendUtils/font/SmileySans-Oblique.ttf  获取当前Repo字体路径

wc = WordCloud(background_color="white",
               width=1920, height=1080, max_words=100,
               font_path=target_path)
wc.generate_from_frequencies(dist)
image = wc.to_image()
image.save('hh.png')
